package com.futu.ai.controller;

import com.futu.ai.pojo.ImageRecognitionRequest;
import com.futu.ai.pojo.ImageRecognitionResponse;
import com.futu.ai.service.AiService;
import com.futu.ai.utils.QiniuUtils;
import com.futu.common.annotation.NoLoginRequired;
import com.futu.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Base64;

/**
 * AI控制器
 *
 * @author AI Assistant
 */
@RestController
@RequestMapping("/api/ai")
@Api(tags = "智普AI接口")
public class AiController {

    @Autowired
    private AiService aiService;

    /**
     * 同步调用AI生成回答
     *
     * @param prompt 提示词
     * @return AI生成的回答
     */
    @PostMapping("/question")
    @ApiOperation("不采用调用AI生成回答")
    @NoLoginRequired
    public String generateQuestion(
            @ApiParam(value = "提示词", required = true) @RequestParam String prompt) {
        return aiService.generateQuestion(prompt);
    }

    /**
     * 流式调用AI生成回答
     *
     * @param prompt 提示词
     * @return SSE响应
     */
    @NoLoginRequired
    @GetMapping(value = "/question/stream", produces = "text/event-stream")
    @ApiOperation("流式调用AI生成回答")
    public SseEmitter generateQuestionStream(
            @ApiParam(value = "提示词", required = true) @RequestParam String prompt) {
        return aiService.generateQuestionSSE(prompt);
    }
    
    /**
     * 图片识别接口 - 识别景点并返回坐标（接收Base64编码图片）
     *
     * @param request 图片识别请求
     * @return 图片识别结果
     */
    @NoLoginRequired
    @PostMapping("/image/recognition")
    @ApiOperation("图片识别接口 - 识别景点并返回坐标（接收Base64编码图片）")
    public Result<ImageRecognitionResponse> recognizeImage(
            @ApiParam(value = "图片识别请求", required = true) @RequestBody ImageRecognitionRequest request) {
        if (StringUtils.isBlank(request.getImageBase64())) {
            return Result.error("图片数据不能为空");
        }
        
        ImageRecognitionResponse response = aiService.recognizeImage(request.getImageBase64());
        if (response.getSuccess() != null && response.getSuccess()) {
            return Result.success(response);
        } else {
            return Result.error(response.getErrorMessage());
        }
    }
    
    /**
     * 图片识别接口 - 上传图片文件
     * 
     * @param file 图片文件
     * @return 识别结果
     */
    @NoLoginRequired
    @PostMapping("/image/upload")
    @ApiOperation("图片识别接口 - 上传图片文件")
    public Result<ImageRecognitionResponse> uploadAndRecognize(
            @ApiParam(value = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        try {
            // 读取文件内容并转换为Base64
            byte[] fileBytes = file.getBytes();
            String base64Image = Base64.getEncoder().encodeToString(fileBytes);
            
            // 调用识别服务
            ImageRecognitionResponse response = aiService.recognizeImage(base64Image);
            if (response.getSuccess() != null && response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(response.getErrorMessage());
            }
        } catch (Exception e) {
            return Result.error("图片上传失败: " + e.getMessage());
        }
    }
} 